package com.lyf.baoxian.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lyf.baoxian.R;
import com.lyf.baoxian.adapter.BaoDanAdapter;
import com.lyf.baoxian.fragment.BaoDanFragment;
import com.lyf.baoxian.fragment.TouBaoFragment;
import com.lyf.baoxian.fragment.CustomerFragment;
import com.lyf.baoxian.fragment.FragmentInstanceManager;
import com.lyf.baoxian.fragment.HomeFragment;
import com.lyf.baoxian.fragment.MyFragment;
import com.lyf.baoxian.fragment.TaoCanFragment;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private FragmentManager fragmentManager;

    private RadioGroup radio_group;

    private HomeFragment homeFragment;
    private TouBaoFragment touBaoFragment;
    private TaoCanFragment taoCanFragment;
    private MyFragment myFragment;
    private CustomerFragment customerFragment;
    private BaoDanFragment baoDanFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager = getSupportFragmentManager();

        radio_group = (RadioGroup) findViewById(R.id.radio_group);
        radio_group.setOnCheckedChangeListener(this);
        //默认选中首页
        radio_group.check(R.id.rb_home);


    }

    /**
     * 切换到底部导航的fragment
     *
     * @param checkedId
     */
    public void switchNavigationFragment(int checkedId) {
        radio_group.check(checkedId);
    }


    /**
     * 切换fragment的方法，点击RadioButton的时候清空回退栈
     *
     * @param fragment
     */
    public void switchFragment(Fragment fragment) {
        //开启事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //循环回退栈
        int backStackEntryCount = fragmentManager.getBackStackEntryCount();
        while (backStackEntryCount > 0) {
            fragmentManager.popBackStack();
            backStackEntryCount--;
        }

        transaction.replace(R.id.fl_container, fragment);
        transaction.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
            case R.id.rb_home: //首页
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                }
//                switchFragment(FragmentInstanceManager.getInstance().getFragment(HomeFragment.class));
                switchFragment(new HomeFragment());
                break;
            case R.id.rb_bx:  //保单
                if (touBaoFragment == null) {
                    touBaoFragment = new TouBaoFragment();
                }

                if (baoDanFragment == null) {
                    baoDanFragment = new BaoDanFragment();
                }
//                switchFragment(FragmentInstanceManager.getInstance().getFragment(TouBaoFragment.class));
                switchFragment(baoDanFragment);
                break;
            case R.id.rb_taocan: //套餐
                if (taoCanFragment == null) {
                    taoCanFragment = new TaoCanFragment();
                }

                switchFragment(taoCanFragment);
//                switchFragment(FragmentInstanceManager.getInstance().getFragment(TaoCanFragment.class));
                break;
            case R.id.rb_customer:  //客户
                if (customerFragment == null) {
                    customerFragment = new CustomerFragment();
                }
                switchFragment(customerFragment);
//                switchFragment(FragmentInstanceManager.getInstance().getFragment(CustomerFragment.class));
                break;
            case R.id.rb_my:  //我的
                if (myFragment == null) {
                    myFragment = new MyFragment();
                }
                switchFragment(myFragment);
//                switchFragment(FragmentInstanceManager.getInstance().getFragment(MyFragment.class));
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new AlertDialog.Builder(MainActivity.this).setTitle("真的要离开")
                    .setMessage("确定要离开")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            MainActivity.this.finish();

                        }
                    }).setNegativeButton("取消", null).show();

        }
        return super.onKeyDown(keyCode, event);
    }

}
